<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false" top="20px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="140px">
      <el-form-item prop="name" :label="$t('company.name')">
        <el-input v-model="dataForm.name" :placeholder="$t('company.name')"/>
      </el-form-item>
      <el-form-item prop="regCode" :label="$t('company.regCode')">
        <el-input v-model="dataForm.regCode" :placeholder="$t('company.regCode')"/>
      </el-form-item>
      <el-form-item prop="legalPerson" :label="$t('company.legalPerson')">
        <el-input v-model="dataForm.legalPerson" :placeholder="$t('company.legalPerson')"/>
      </el-form-item>
      <el-form-item prop="legalPersonMobile" :label="$t('company.legalPersonMobile')">
        <el-input v-model="dataForm.legalPersonMobile" :placeholder="$t('company.legalPersonMobileDes')" />
      </el-form-item>
      <el-form-item prop="contactMobile" :label="$t('company.contactMobile')">
        <el-input v-model="dataForm.contactMobile" :placeholder="$t('company.contactMobile')" />
      </el-form-item>
      <el-form-item prop="contactMail" :label="$t('company.contactMail')">
        <el-input v-model="dataForm.contactMail" :placeholder="$t('company.contactMail')" />
      </el-form-item>
      <el-form-item prop="address" :label="$t('company.address')">
        <el-input v-model="dataForm.address" :placeholder="$t('company.address')"/>
      </el-form-item>
      <el-form-item prop="remark" :label="$t('remark')">
        <el-input type="textarea" v-model="dataForm.remark" :placeholder="$t('remark')"/>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { debounce } from 'lodash'
  export default {
    data () {
      return {
        visible: false,
        menuList: [],
        deptList: [],
        dataForm: {
          id: '',
          name: '',
          regCode: '',
          legalPerson:'',
          legalPersonMobile:'',
          contactMobile:'',
          contactMail:'',
          address: '',
          remark:''
        }
      }
    },
    computed: {
      dataRule () {
        return {
          name: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ],
          regCode: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ],
          contactMobile: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ]

        }
      }
    },
    methods: {
      init () {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          Promise.all([

          ]).then(() => {
            if (this.dataForm.id) {
              this.getInfo()
            }
          })
        })
      },
      // 获取信息
      getInfo () {
        this.$axios.get(`/manager/company/info/${this.dataForm.id}`).then(res => {
          this.dataForm = {
            ...this.dataForm,
            ...res
          }
          this.dataForm.menuIdList.forEach(item => this.$refs.menuListTree.setChecked(item, true))
          this.$refs.deptListTree.setCheckedKeys(this.dataForm.deptIdList)
        }).catch(() => {})
      },
      // 表单提交
      dataFormSubmitHandle: debounce(function () {
        this.$refs['dataForm'].validate((valid) => {
          if (!valid) {
            return false
          }
          // this.dataForm.menuIdList = JSON.stringify(this.dataForm.menuIdList)
          this.$http.json('/manager/company/addOrUpdate', this.dataForm).then(res => {
            this.$message({
              message: this.$t('prompt.success'),
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }).catch(() => {})
        })
      }, 1000, { 'leading': true, 'trailing': false })
    }
  }
</script>
